package com.cincc.cinccimdemo.common;

import com.cincc.cinccimdemo.model.FriendInfoUI;
import com.cincc.cinccimdemo.model.GroupMemberInfoUI;
import com.cincc.cinccimdemo.widget.SideBase;
import com.cincc.im.model.dbmodel.FriendInfo;
import com.cincc.im.model.dbmodel.GroupMemberInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class ContactUtil {
    public static String[] LETTER_LIST = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
            "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};

    /**
     * 根据当前选中的项获取其第一次出现该项首字母的位置
     *
     * @param position 当前选中的位置
     * @param datas    数据源
     * @return
     */
    public static int getPositionForSection(int position, List<? extends SideBase> datas) {
        // 当前选中的项
        SideBase sideBase = datas.get(position);
        for (int i = 0; i < datas.size(); i++) {
            String firstStr = datas.get(i).getLetterName().toUpperCase();
            // 返回第一次出现该项首字母的位置
            if (firstStr.equals(sideBase.getLetterName())) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 根据当前选中的项获取其第一次出现该项首字母的位置
     *
     * @param position   当前选中的位置
     * @param startIndex 其实位置 为兼容listview里面有header
     * @param datas      数据源
     * @return
     */
    public static int getPositionForSection(int position, int startIndex, List<? extends SideBase> datas) {
        // 当前选中的项
        SideBase sideBase = datas.get(position);
        for (int i = startIndex; i < datas.size(); i++) {
            String firstStr = datas.get(i).getLetterName().toUpperCase();
            // 返回第一次出现该项首字母的位置
            if (firstStr.equals(sideBase.getLetterName())) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 获取所选中的索引在列表中的位置
     *
     * @param list
     * @param letter
     * @return
     */
    public static int getLetterPosition(List<? extends SideBase> list, String letter) {
        int position = -1;

        if (list != null && !list.isEmpty() && !"".equals(letter)) {
            for (int i = 0; i < list.size(); i++) {
                SideBase bean = list.get(i);
                if (bean.getLetterName().equals(letter)) {
                    position = i;
                    break;
                }
            }
        }
        return position;
    }

    /**
     * 获取所选中的索引在列表中的位置
     *
     * @param list
     * @param letter
     * @param startIndex 为兼容listview有header情况
     * @return
     */
    public static int getLetterPosition(List<? extends SideBase> list, String letter, int startIndex) {
        int position = -1;

        if (list != null && !list.isEmpty() && !"".equals(letter)) {
            for (int i = startIndex; i < list.size(); i++) {
                SideBase bean = list.get(i);
                if (bean.getLetterName().equals(letter)) {
                    position = i;
                    break;
                }
            }
        }
        return position;
    }

    /**
     * 筛选出数据源中所包含的全部索引值
     *
     * @param list
     * @return
     */
    public static String[] getLetters(List<? extends SideBase> list) {
        List<String> letters = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                if (!letters.contains(list.get(i).getLetterName())) {
                    letters.add(list.get(i).getLetterName());
                }
            }
        }
        return (String[]) letters.toArray(new String[letters.size()]);
    }

    /**
     * 筛选出数据源中所包含的全部索引值
     *
     * @param startIndex 为兼容listview有header情况
     * @param list
     * @return
     */
    public static String[] getLetters(int startIndex, List<? extends SideBase> list) {
        List<String> letters = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            for (int i = startIndex; i < list.size(); i++) {
                if (!letters.contains(list.get(i).getLetterName())) {
                    letters.add(list.get(i).getLetterName());
                }
            }
        }
        return (String[]) letters.toArray(new String[letters.size()]);
    }

    public static FriendInfoUI getFriendUI(FriendInfo friendInfo) {
        FriendInfoUI item = new FriendInfoUI(friendInfo);
        //首字母
        String contactHeader = PinyinUtil.getFirstUpperLetter(item.nickName);
        //如果首字母不常规设置为#
        if (!Arrays.asList(ContactUtil.LETTER_LIST).contains(contactHeader)) {
            contactHeader = "#";
        }
        item.contactHeader = contactHeader;
        return item;
    }

    public static GroupMemberInfoUI getGroupMemberInfoUI(GroupMemberInfo friendInfo) {
        GroupMemberInfoUI item = new GroupMemberInfoUI(friendInfo);
        //首字母
        String contactHeader = PinyinUtil.getFirstUpperLetter(item.nickName);
        //如果首字母不常规设置为#
        if (!Arrays.asList(ContactUtil.LETTER_LIST).contains(contactHeader)) {
            contactHeader = "#";
        }
        item.contactHeader = contactHeader;
        return item;
    }

    public static List<FriendInfoUI> queryByContactHeader(List<FriendInfoUI> allContacts, String letter) {
        return allContacts.stream()
                .filter(object -> letter.equals(object.contactHeader))
                .sorted(Comparator.comparing(FriendInfoUI::getNickName))
                .collect(Collectors.toList());
    }

    public static List<GroupMemberInfoUI> queryByGroupMemberHeader(List<GroupMemberInfoUI> allContacts, String letter) {
        return allContacts.stream()
                .filter(object -> letter.equals(object.contactHeader))
                .sorted(Comparator.comparing(GroupMemberInfoUI::getNickName))
                .collect(Collectors.toList());
    }
}
